/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.odg.m_ui.beans;

import java.util.LinkedHashSet;
import java.util.Set;
import org.opu.odg.dg.incomingdata.DatabaseSettings;

/**
 *
 * @author yura
 */
public class ContentBean extends AbstractBean {

    public static enum ContentType {
        Custom, Database
    }

    private ContentBean parent;
    private Set<ContentBean> childrens = new LinkedHashSet<ContentBean>();
    private Set<VariableBean> variables = new LinkedHashSet<VariableBean>();

    // model
    private ContentType contentType;
    private boolean describesExternalTag;
    private DatabaseSettings databaseSettings;
    private String query;

    public ContentBean getParent() {
        return parent;
    }

    public void setParent(ContentBean parent) {
        this.parent = parent;
    }

    public Set<ContentBean> getChildrens() {
        return childrens;
    }

    public void setChildrens(Set<ContentBean> childrens) {
        this.childrens = childrens;
    }

    public void addChildren(ContentBean children) {
        this.childrens.add(children);
        children.setParent(this);
    }

    public Set<VariableBean> getVariables() {
        return variables;
    }

    public void setVariables(Set<VariableBean> variables) {
        this.variables = variables;
    }

    public void addVariable(VariableBean variable) {
        this.variables.add(variable);
    }

    public VariableBean getVariable(String name) {
        for (VariableBean object : variables) {
            if (object.getName().equals(name)) {
                return object;
            }
        }
        for (ContentBean contentBean : childrens) {
            VariableBean variable = contentBean.getVariable(name);
            if (variable != null) {
                return variable;
            }
        }
        return null;
    }

    public ContentType getContentType() {
        if (contentType == null) {
            contentType = ContentType.Custom;
        }
        return contentType;
    }

    public void setContentType(ContentType contentType) {
        this.contentType = contentType;
    }

    public DatabaseSettings getDatabaseSettings() {
        if (databaseSettings == null) {
            databaseSettings = new DatabaseSettings("", "", "", "");
        }
        return databaseSettings;
    }

    public void setDatabaseSettings(DatabaseSettings databaseSettings) {
        this.databaseSettings = databaseSettings;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public boolean isDescribesExternalTag() {
        return describesExternalTag;
    }

    public void setDescribesExternalTag(boolean describesExternalTag) {
        this.describesExternalTag = describesExternalTag;
    }
}
